package com.maven.redis;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQTextMessage;
import org.junit.Test;
import org.omg.PortableInterceptor.ACTIVE;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPool;

import javax.jms.*;
import java.util.HashSet;
import java.util.Set;

public class RedisTest {

    //redis是单利的
    @Test
    public void test1(){

        Jedis jedis = new Jedis("192.168.56.102",6379);
        jedis.set("good","kkk");
        jedis.get("good");
        jedis.close();

    }

    //连接池
    @Test
    public void test2(){
        JedisPool jedisPool = new JedisPool("192.168.56.102",6379);
        Jedis resource = jedisPool.getResource();
        String good = resource.get("good");
        System.out.println(good);

    }

  //集群连接
    @Test
    public void test3(){
        Set<HostAndPort> set = new HashSet<HostAndPort>();
        set.add(new HostAndPort("192.168.56.102",7001));
        set.add(new HostAndPort("192.168.56.102",7002));
        set.add(new HostAndPort("192.168.56.102",7003));
        set.add(new HostAndPort("192.168.56.102",7004));
        set.add(new HostAndPort("192.168.56.102",7005));
        set.add(new HostAndPort("192.168.56.102",7006));

        JedisCluster jedisCluster = new JedisCluster(set);
        String good = jedisCluster.get("liang");
        System.out.println(good);

    }

    /*active的发布订阅测试，在项目是配置在spring*/
    //发布，订阅
    @Test
    public void test4() throws JMSException {

        ConnectionFactory connectionFactory= new ActiveMQConnectionFactory("tcp://192.168.56.102:61616");
        Connection connection = connectionFactory.createConnection();
        connection.start();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Queue queue = session.createQueue("test-query");
        MessageProducer producer = session.createProducer(queue);
       /* ActiveMQTextMessage activeMQTextMessage = new ActiveMQTextMessage();
        activeMQTextMessage.setText("hello world!");*/

        TextMessage textMessage = session.createTextMessage("hello world!");
        producer.send(textMessage);
        session.close();
        producer.close();
        connection.close();

    }


}
